package com.xzj.admin.analysis.base.custom.it;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.xzj.admin.analysis.base.common.handle.CommonAbstractAnalysisHandle;
import com.xzj.admin.analysis.base.constants.CommonEnums;
import com.xzj.admin.analysis.base.custom.common.CommonAnalysis;
import com.xzj.admin.analysis.base.util.AnalysisCommonUtil;

/**
 * @program: xzj-admin
 * @ClassName: FcAnalysisHandle
 * @description: IT云FC
 * @author: XiaZunJun
 * @create: 2024-11-12 09:02
 */
public class FcAnalysisHandle extends CommonAbstractAnalysisHandle {
    
    static {
        registerJudgeSubFun(CommonEnums.RESOURCE_TYPE.host.name(), FcAnalysisHandle::hostSub);
        registerJudgeSubFun(CommonEnums.RESOURCE_TYPE.vm.name(), FcAnalysisHandle::vmSub);
        registerJudgeSubFun(CommonEnums.RESOURCE_TYPE.storageUnit.name(), FcAnalysisHandle::storageUnitSub);
        registerJudgeSubFun(CommonEnums.RESOURCE_TYPE.region.name(), CommonAnalysis::regionSub);
        registerJudgeSubFun(CommonEnums.RESOURCE_TYPE.storage.name(), FcAnalysisHandle::storageSub);
    }

    public FcAnalysisHandle(String cloudId,String cloudType) {
        super(cloudId, cloudType,new FcThemeSpecialAnalysisHandle(cloudId,cloudType));
    }
    /**
     * 构建依托宿主机下的宿主机与集群关系
     *
     * @param row          父级节点解析对象体
     * @param parentId     对应父级节点Id
     * @param fieldName    对应子集字段名
     * @param subThemeRows 子数据源解析对象
     * @author xiazunjun
     * @date 2023/11/13 14:50
     */
    private static void hostSub(JSONObject row, String parentId, String fieldName, JSONArray subThemeRows) {
        // 构建依托宿主机下的宿主机与集群关系
        JSONObject subJson = new JSONObject();
        subJson.put("clusterId", row.get(fieldName));
        subJson.put("hostId", parentId);
        subJson.put("id", parentId);
        subThemeRows.add(subJson);
    }

    /**
     * 构建依托虚机下的虚机与磁盘关系
     *
     * @param row          父级节点解析对象体
     * @param parentId     对应父级节点Id
     * @param fieldName    对应子集字段名
     * @param subThemeRows 子数据源解析对象
     * @author xiazunjun
     * @date 2023/12/21 14:50
     */
    private static void vmSub(JSONObject row, String parentId, String fieldName, JSONArray subThemeRows) {
        if (fieldName.contains(":")) {
            String[] split = fieldName.split(":");
            // 一直解析需要两层
            JSONArray subJsons = row.getJSONObject(split[0]).getJSONArray(split[1]);
            for (int i = 0; i < subJsons.size(); i++) {
                JSONObject subJson = new JSONObject();
                String volumeUrn = subJsons.getJSONObject(i).getString("volumeUrn");
                subJson.put("volumeUrn", volumeUrn);
                subJson.put("vmUrn", parentId);
                subJson.put("id", AnalysisCommonUtil.commonBuildStr(parentId, volumeUrn));
                subThemeRows.add(subJson);
            }
        }
    }

    /**
     * 构建依托存储设备下的存储设备与主机关系
     *
     * @param row          父级节点解析对象体
     * @param parentId     对应父级节点Id
     * @param fieldName    对应子集字段名
     * @param subThemeRows 子数据源解析对象
     * @author xiazunjun
     * @date 2023/12/21 14:50
     */
    private static void storageUnitSub(JSONObject row, String parentId, String fieldName, JSONArray subThemeRows) {
        JSONArray subJsons = row.getJSONArray(fieldName);
        for (int i = 0; i < subJsons.size(); i++) {
            JSONObject subJson = subJsons.getJSONObject(i);
            String hostId = subJson.getString("urn");
            subJson.put("storageUnitId", parentId);
            subJson.put("id", AnalysisCommonUtil.commonBuildStr(parentId, hostId));
            subThemeRows.add(subJson);
        }
    }

    /**
     * 构建依托与虚拟存储池的 设备与虚拟存储池关系
     *
     * @param row          父级节点解析对象体
     * @param parentId     对应父级节点Id
     * @param fieldName    对应子集字段名
     * @param subThemeRows 子数据源解析对象
     * @author xiazunjun
     * @date 2023/11/13 14:50
     */
    private static void storageSub(JSONObject row, String parentId, String fieldName, JSONArray subThemeRows) {
        if ("suUrn".equals(fieldName)) {
            JSONObject subJson = new JSONObject();
            String unitId = row.getString(fieldName);
            subJson.put("id", AnalysisCommonUtil.commonBuildStr(parentId, unitId));
            subJson.put("unitId", unitId);
            subJson.put("storagePoolId", parentId);
            subJson.put("specId", "12580001");
            subJson.put("isMasterLun", "10050001");
            subThemeRows.add(subJson);
        } else {
            JSONArray array = row.getJSONArray(fieldName);
            if (!array.isEmpty()) {
                for (int i = 0; i < array.size(); i++) {
                    JSONObject subJson = new JSONObject();
                    String deviceId = array.getString(i);
                    subJson.put("id", AnalysisCommonUtil.commonBuildStr(parentId, deviceId));
                    subJson.put("deviceId", deviceId);
                    subJson.put("storagePoolId", parentId);
                    subJson.put("specId", "12580002");
                    subThemeRows.add(subJson);
                }
            }
        }
    }
}
